<?php

include $_SERVER['DOCUMENT_ROOT'] . '/config.php';

class DataProvider {

    public static $LAST_INSERT_ID = 0;

    public static function ExecuteQuery($sql) {
        $connection = mysql_connect(DB_HOST, DB_USER, DB_PASS) or
                die("couldn't connect to localhost");

        // ebookDB : Tên CSDL   
        mysql_select_db(DB_NAME, $connection);

        mysql_query("set names 'utf8'");

        $result = mysql_query($sql, $connection);
        self::$LAST_INSERT_ID = mysql_insert_id($connection);

        mysql_close($connection);

        return $result;
    }

    public static function GetOneRow($sql) {
        $connection = mysql_connect(DB_HOST, DB_USER, DB_PASS) or
                die("couldn't connect to localhost");

        // user : Tên CSDL   
        mysql_select_db(DB_NAME, $connection);

        mysql_query("set names 'utf8'");

        $result = mysql_query($sql, $connection);
        $row = mysql_fetch_array($result);

        mysql_close($connection);

        return $row;
    }

    public static function GetOneRowi($sql) {
        $connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME)
                or die("couldn't connect to localhost");

        $result = mysqli_query($connection, $sql);
        $row = mysqli_fetch_array($result, MYSQLI_NUM);

        return $row[0];
    }

    public static function ExecuteQueryArray($sql) {
        $connection = mysql_connect(DB_HOST, DB_USER, DB_PASS) or
                die("couldn't connect to localhost");

        mysql_select_db(DB_NAME, $connection);

        mysql_query("set names 'utf8'");

        $kq = mysql_fetch_array($sql);

        mysql_close($connection);

        return $kq;
    }

    public static function TongSanPham($sql) {
        $connection = mysql_connect(DB_HOST, DB_USER, DB_PASS) or
                die("couldn't connect to localhost");

        mysql_select_db(DB_NAME, $connection);

        mysql_query("set names 'utf8'");

        $result = mysql_query($sql, $connection);
        $kq = mysql_result($result, 0);

        mysql_close($connection);

        return $kq;
    }

    public static function GetRows($sql) {
        $connection = mysql_connect(DB_HOST, DB_USER, DB_PASS) or
                die("couldn't connect to localhost");

        mysql_select_db(DB_NAME, $connection);

        mysql_query("set names 'utf8'");

        $result = mysql_query($sql, $connection);
        $i = 0;
        while ($row = mysql_fetch_array($result)) {
            $returnresult[$i++] = $row;
        }
        mysql_close($connection);
        return $returnresult;
    }

}

?> 